import org.apache.log4j.Logger;
import org.hibernate.*;
import java.util.*;

import hibernate.*;

public class Main {
	private Session session;

	private Logger log;

	/**
	 * Chaz Beck ComS 461 Assignment 4
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Main m = new Main();
		try {
			m.setUp();
			m.queryInstructorComS();
			m.changeInstructorAddress();
			m.removeInstructors();
			m.queryInstructorComS();
			m.queryInstructorAmes();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	private void queryInstructorComS() {
		Transaction tx = null;
		log.info("#### Querying Instructors in ComS");
		try {
			tx = session.beginTransaction();

			System.out.println("Name\t\tAddress");

			List list = session
					.createQuery(
							"select instruct from Instructor as instruct where instruct.program='ComS'")
					.list();
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				Instructor instructor = (Instructor) iter.next();
				System.out.print(instructor.getName() + "\t");
				System.out.println(instructor.getInstructorAdd());
			}
			System.out.println("\n");

			tx.commit();
			log.info("#### Successful at Querying Instructors in ComS");
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			System.err
					.println("Experienced an error with Hibernate during instructor query for ComS: \n"
							+ e.getMessage());
		}
	}

	private void queryInstructorAmes() {
		Transaction tx = null;
		log.info("#### Querying Instructors living in Ames");
		try {
			tx = session.beginTransaction();

			System.out.println("Name\t\tAddress");

			List list = session
					.createQuery(
							"select instruct from Instructor as instruct where instruct.instructorAdd like 'Ames%'")
					.list();
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				Instructor instructor = (Instructor) iter.next();
				System.out.print(instructor.getName() + "\t");
				System.out.println(instructor.getInstructorAdd());
			}
			System.out.println("\n");

			tx.commit();
			log.info("#### Successful at Querying Instructors in Ames");
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			System.err
					.println("Experienced an error with Hibernate during instructor query for Ames: \n"
							+ e.getMessage());
		}
	}

	private void changeInstructorAddress() {
		Transaction tx = null;
		log.info("#### Changing Instructor ID=1030 Address");
		try {
			tx = session.beginTransaction();

			List list = session
					.createQuery(
							"select instruct from Instructor as instruct where instruct.instructorId=1030")
					.list();
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				Instructor instructor = (Instructor) iter.next();
				instructor.setInstructorAdd("Ames, IA");
			}
			session.flush();

			tx.commit();
			log.info("#### Successful at Changing Instructor ID=1030 Address");
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			System.err
					.println("Experienced an error with Hibernate during instructor modification: \n"
							+ e.getMessage());
		}
	}

	private void removeInstructors() {
		Transaction tx = null;
		log.info("#### Removing instructors ID=1040,1050");
		try {
			tx = session.beginTransaction();

			List list = session
					.createQuery(
							"select instruct from Instructor as instruct where instruct.instructorId=1040 or instruct.instructorId=1050")
					.list();
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				Instructor instructor = (Instructor) iter.next();

				Set courses = instructor.getCourses();
				for (Iterator iter2 = courses.iterator(); iter2.hasNext();) {
					Course course = (Course) iter2.next();
					session.delete(course);
				}

				session.delete(instructor);
			}

			// session.createQuery("select instruct from Instructor as instruct where instruct.instructorId=1040 or instruct.instructorId=1050").list().clear();

			// Query q =
			// session.createQuery("delete Instructor as instruct where instruct.instructorId=1040 or instruct.instructorId=1050");
			// q.executeUpdate();

			tx.commit();
			log.info("#### Successful at Removing instructors ID=1040,1050");
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			System.err
					.println("Experienced an error with Hibernate during instructor removal: \n"
							+ e.getMessage());
		}
	}

	private void setUp() throws Exception {
		session = HibernateSessionFactory.getSession();
		log = Logger.getLogger(this.getClass());
	}

}
